package com.test.uiapp2.function.Camera;

import android.database.sqlite.SQLiteException;
import android.util.Log;

import androidx.test.uiautomator.UiObject;
import androidx.test.uiautomator.UiObjectNotFoundException;
import androidx.test.uiautomator.UiSelector;

import com.test.uiapp2.base.TestBase;
import com.test.uiapp2.element.Base.BaseElement;
import com.test.uiapp2.element.Camera.CameraElement;

public class CameraFcuntion extends TestBase {

    //前置摄像头-录像
    public  void  FrontVideo(){
        try {
            String FrontCameraName="前置摄像头";
            int cameraVideoSize = getCameraVideoSize();
            Log.i(TAG, "拍摄前视频个数:"+cameraVideoSize+"个");
            sleep(WAIT);
            swipe_right();
            sleep(WAITTIME);
            startApp(CameraElement.CAMERANAME);
            if(getId(BaseElement.CAMERA2,CameraElement.SHUTTER_BUTTON).exists()) {
                String contentDescription = getId(BaseElement.CAMERA2, CameraElement.BUTTON_DREAM).getContentDescription();
                if (!contentDescription.equals(FrontCameraName)) {
                    Log.i(TAG, "当前不是前置摄像头");
                    clickById(BaseElement.CAMERA2, CameraElement.BUTTON_DREAM);
                    sleep(MWAITTIME);
                }
                UiObject make = getId(BaseElement.CAMERA2,CameraElement.BTN_MODE);
                if(make.exists()){
                    clickById(BaseElement.CAMERA2,CameraElement.BTN_MODE);
                    sleep(WAITTIME);
                    verify("摄像模式未切换成功",!getId(BaseElement.CAMERA2,CameraElement.VIDEO_FLASH).exists());
                }
                StartRecording(cameraVideoSize);
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }



    //后置摄像头-录像
    public void RearVideo(){
        try {
            String FrontCameraName="后置摄像头";
            int cameraVideoSize = getCameraVideoSize();
            Log.i(TAG, "拍摄前视频个数:"+cameraVideoSize+"个");
            sleep(WAIT);
            swipe_right();
            sleep(WAITTIME);
            startApp(CameraElement.CAMERANAME);
            if(getId(BaseElement.CAMERA2,CameraElement.SHUTTER_BUTTON).exists()){
                String contentDescription = getId(BaseElement.CAMERA2, CameraElement.BUTTON_DREAM).getContentDescription();
                if(!contentDescription.equals(FrontCameraName)){
                    Log.i(TAG, "当前不是后置摄像头");
                    clickById(BaseElement.CAMERA2,CameraElement.BUTTON_DREAM);
                    sleep(MWAITTIME);
                }
                UiObject make = getId(BaseElement.CAMERA2,CameraElement.BTN_MODE);
                if(make.exists()){
                    clickById(BaseElement.CAMERA2,CameraElement.BTN_MODE);
                    sleep(WAITTIME);
                    verify("摄像模式未切换成功",getId(BaseElement.CAMERA2,CameraElement.VIDEO_FLASH).exists());
                }
                StartRecording(cameraVideoSize);
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }

    //录像
    public void StartRecording(int cameraVideoSize){
        try {
            sleep(WAITTIME);
            Log.i(TAG, "开始录像啦~~");
            UiObject shutter_button = getId(BaseElement.CAMERA2, CameraElement.SHUTTER_BUTTON);
            if(shutter_button.exists()){
                for(int i=1;i<=2;i++){
                    Log.i(TAG, "开始录制第"+i+"个视频");
                    sleep(WAITTIME);
                    shutter_button.click();
                    sleep(MWAITTIME*3);
                    shutter_button.click();
                }
            }
            int VideoSize = cameraVideoSize + 2;
            Log.i(TAG, "拍摄后视频个数应该是:"+VideoSize+"个");
//          //左滑退出
            swipe_right();
//          minimize();
            sleep(WAITTIME);
            int i = getCameraVideoSize();
            Log.i(TAG, "拍摄后相册视频是:"+i+"个");
            verify("拍摄后，相册视频数据不对",VideoSize==i);
            sleep(WAITTIME);
            swipe_right();

        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }


    //后置摄像头-连续拍照
    public void ContinuousPhotography(){
        try {
            String FrontCameraName="后置摄像头";
            int cameraSize = getCameraSize();
            Log.i(TAG, "相册拍照前数量为"+cameraSize+"张");
            sleep(WAIT);
            swipe_right();
            sleep(WAITTIME);
            startApp(CameraElement.CAMERANAME);
            sleep(WAITTIME);
            if(getId(BaseElement.CAMERA2,CameraElement.SHUTTER_BUTTON).exists()){
                Log.i(TAG, "ContinuousPhotography: 进入连续拍照方法");
                String contentDescription = getId(BaseElement.CAMERA2, CameraElement.BUTTON_DREAM).getContentDescription();
                if(!contentDescription.equals(FrontCameraName)){
                    Log.i(TAG, "当前不是后置摄像头");
                    clickById(BaseElement.CAMERA2,CameraElement.BUTTON_DREAM);
                    sleep(SHOTWAIT*10);
                }
                sleep(WAITTIME);
                swipe_left();
                sleep(WAITTIME);
                if(getTextContains(CameraElement.CONTINUOUSPHOTOGRAPHY).exists()){
                    clickByText(CameraElement.CONTINUOUSPHOTOGRAPHY);
                    sleep(WAITTIME);
                }
                for (int i=0;i<5;i++){
                    UiObject id = getId(BaseElement.CAMERA2, CameraElement.SHUTTER_BUTTON);
                    longClick(uiDevice,id,300);
                    sleep(MWAITTIME);
                }
                int i = cameraSize + 5;
                Log.i(TAG, "Camera_pz: 拍照后相册数量应该为="+i+"张");
                int cameraSize1 = getCameraSize();
                Log.i(TAG, "Camera_pz: 拍照后相册数量为="+cameraSize1+"张");
                verify("拍照的数量错误",i==cameraSize1);
                sleep(WAITTIME);
                minimize();
            }else{
                Log.i(TAG, "RearCamera: 未进入相机界面");
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }

    //后摄像头拍照
    public  void  RearCamera(){
        try {
            String FrontCameraName="后置摄像头";
            int cameraSize = getCameraSize();
            Log.i(TAG, "相册当前数量为"+cameraSize+"张");
            sleep(WAIT);
            swipe_right();
            sleep(WAITTIME);
            startApp(CameraElement.CAMERANAME);
            sleep(WAITTIME*2);
            if(getId(BaseElement.CAMERA2,CameraElement.SHUTTER_BUTTON).exists()){
                String contentDescription = getId(BaseElement.CAMERA2, CameraElement.BUTTON_DREAM).getContentDescription();
                Log.i(TAG, "contentDescription="+contentDescription);
                if(!contentDescription.equals(FrontCameraName)){
                    Log.i(TAG, "FrontCamera: 当前不是后置摄像头");
                    //当前不是前置摄像头，切换摄像头
                    clickById(BaseElement.CAMERA2,CameraElement.BUTTON_DREAM);
                    sleep(SHOTWAIT*10);
                }
                for (int i=0;i<2;i++){
                    clickById(BaseElement.CAMERA2,CameraElement.SHUTTER_BUTTON);
                    sleep(MWAITTIME);
                }
                int i = cameraSize + 2;
                Log.i(TAG, "Camera_pz: 拍照后相册数量应该为="+i+"张");
                int cameraSize1 = getCameraSize();
                Log.i(TAG, "Camera_pz: 拍照后相册数量为="+cameraSize1+"张");
                verify("拍照的数量错误",i==cameraSize1);
                sleep(WAITTIME);
                minimize();
            }else{
                RearCamera();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //前摄像头拍照
    public void FrontCamera(){
        try {
            String FrontCameraName="前置摄像头";
            int cameraSize = getCameraSize();
            Log.i(TAG, "相册当前数量为"+cameraSize+"张");
            sleep(WAIT);
            swipe_right();
            sleep(WAITTIME);
            startApp(CameraElement.CAMERANAME);
            sleep(WAITTIME*2);
            if(getId(BaseElement.CAMERA2,CameraElement.BUTTON_DREAM).exists()){
//                //切换摄像头
//                clickById(BaseElement.CAMERA2,CameraElement.BUTTON_DREAM);
//                sleep(WAITTIME);
                String contentDescription = getId(BaseElement.CAMERA2, CameraElement.BUTTON_DREAM).getContentDescription();
                Log.i(TAG, "contentDescription="+contentDescription);
                if(!contentDescription.equals(FrontCameraName)){
                    Log.i(TAG, "FrontCamera: 当前不是前置摄像头");
                    //当前不是前置摄像头，切换摄像头
                    clickById(BaseElement.CAMERA2,CameraElement.BUTTON_DREAM);
                    sleep(SHOTWAIT*10);
                }
                sleep(WAIT);
                if(getId(BaseElement.CAMERA2,CameraElement.SHUTTER_BUTTON).exists()){
                    for (int i=0;i<2;i++){
                        clickById(BaseElement.CAMERA2,CameraElement.SHUTTER_BUTTON);
                        sleep(MWAITTIME);
                    }
                    int i = cameraSize + 2;
                    Log.i(TAG, "Camera_pz: 拍照后相册数量应该为="+i+"张");
                    int cameraSize1 = getCameraSize();
                    Log.i(TAG, "Camera_pz: 拍照后相册数量为="+cameraSize1+"张");
                    verify("拍照的数量错误",i==cameraSize1);
                    sleep(WAITTIME);
                    minimize();
                }
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }


    //切换相机模式
    public void  SwitchCameraMode(){
        startApp(CameraElement.CAMERANAME);
        sleep(WAITTIME);
        swipe_left();
        if(getTextContains(CameraElement.AUTOMATIC).exists()){
            sleep(WAIT);
            clickByText(CameraElement.AUTOMATIC);
            sleep(WAITTIME);
            UiObject filter = getId(BaseElement.CAMERA2, CameraElement.FILTER_TOGGLE);
            verify("切换自动相机模式失败",filter.exists());
            sleep(WAITTIME);
            swipe_right();
        }

    }

    //相册删除照片
    public void DeletePicture(){
        try {
            int cameraSize = getCameraSize();
            Log.i(TAG, "Camera_pz: 删除前数量应该为="+cameraSize+"张");
            sleep(WAITTIME);
            clickByText(CameraElement.CAMERANAME);
            CameraDeletePicture(cameraSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //相机删除照片
    public void  CameraDeletePicture(int cameraSize){
        try {
            sleep(WAITTIME);
            UiObject ParentNodeId = getId(BaseElement.GALLERY3D, CameraElement.PAGE_V2);
            if(ParentNodeId.exists()){
                UiObject child0 = ParentNodeId.getChild(new UiSelector().resourceId(CameraElement.IMAGE_CONTAINER).index(1));
                longClick(uiDevice,child0,200);
                sleep(WAIT);
                for(int i=2;i<ParentNodeId.getChildCount();i++){
                    UiObject child = ParentNodeId.getChild(new UiSelector().resourceId(CameraElement.IMAGE_CONTAINER).index(i));
                    child.click();
                    sleep(WAIT);
                    if(i==2){
                        break;
                    }
                }
                UiObject descContains = getDescContains(CameraElement.DELETEPICTURE);
                if(descContains.exists()){
                    clickByDescContains(CameraElement.DELETEPICTURE);
                    ConfirmPop_ups();
                    swipe_right();
                    sleep(WAITTIME*2);
                    int i = cameraSize - 2;
                    sleep(WAIT);
                    getClassName(CameraElement.IMAGEBUTTON).click();
                    Log.i(TAG, "删除后的照片个数应该为="+i+"张");
                    int cameraSize1 = getCameraSize();
                    Log.i(TAG, "当前相册照片个数为："+cameraSize1+"张");
                    boolean b = i == cameraSize1;
                    Log.i(TAG, "是否相等: "+b);
                    verify("拍照的数量错误",i == cameraSize1);
                    sleep(WAITTIME);
                    Log.i(TAG, "CameraDeletePicture: 删除完成后点击返回按钮");
                    minimize();
                }
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }

    public  void  ConfirmPop_ups(){
        try {
            sleep(WAIT);
            if(getTest(CameraElement.DELETETS).exists()){
                clickByText(CameraElement.OK);
            }else{
                verify("没有进入删除界面",getTest(CameraElement.DELETETS).exists());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //相册图片数量
    public int getCameraSize(){
        int CameraSize = 0;
        try {
            startApp(CameraElement.GALLERY);
            sleep(WAITTIME);
            if(getTest(CameraElement.GALLERY).exists()){
                clickByText(CameraElement.GALLERY);
                sleep(WAITTIME);
                UiObject id = getId(CameraElement.RECYCLER_VIEW_ALBUMSET_PAGE);
                //算出相册数量
                UiObject xc = id.getChild(new UiSelector().className(CameraElement.RELATIVELAYOUT).index(1));
                UiObject child = xc.getChild(new UiSelector().className(CameraElement.LINEARLAYOUT).index(1));
                String xcsize = child.getChild(new UiSelector().className(CameraElement.TEXTVIEW).index(1)).getText();
                String substring = xcsize.substring(0, xcsize.indexOf(" "));
                int CameraNumber = Integer.parseInt(substring);
                CameraSize = CameraNumber;
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }

        return CameraSize;
    }

    //相册视频数量
    public int getCameraVideoSize(){
        int CameraSize = 0;
        try {
            startApp(CameraElement.GALLERY);
            sleep(WAITTIME);
            if(getTest(CameraElement.GALLERY).exists()){
                clickByText(CameraElement.GALLERY);
                sleep(WAITTIME);
                UiObject id = getId(CameraElement.RECYCLER_VIEW_ALBUMSET_PAGE);
                //算出相册数量
                UiObject xc = id.getChild(new UiSelector().className(CameraElement.RELATIVELAYOUT).index(1));
                UiObject child = xc.getChild(new UiSelector().className(CameraElement.LINEARLAYOUT).index(1));
                String xcsize = child.getChild(new UiSelector().className(CameraElement.TEXTVIEW).index(1)).getText();
                boolean contains = xcsize.contains(CameraElement.VIDEO);
                Log.i(TAG, "判断当前相册-相机类型是否有视频"+contains);
                if(contains!=false){
                    String replace = xcsize.replace(" ", "");
                    Log.i(TAG, "replace="+replace);
                    String substring = xcsize.substring(xcsize.indexOf(",")+1,xcsize.indexOf("个"));
                    String trim = substring.trim();
                    int CameraNumber = Integer.parseInt(trim);
                    CameraSize=CameraNumber;
                    Log.i(TAG, "当前相册-相机类型视频个数="+CameraNumber);
                }else{
                    Log.i(TAG, "当前相册-相机类型视频个数="+CameraSize);
                }
            }
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }

        return CameraSize;
    }



    //向右滑出拍照模式
    public void swipe_left(){
        sleep(WAIT);
        uiDevice.swipe(148,852,909,818,10);
    }
    //想左滑出，返回按钮
   public void swipe_right(){
        sleep(WAITTIME);
//       uiDevice.swipe(1289,1648,1042,1670,STEP);
       uiDevice.swipe(1484,1302,394,1328,STEP);
   }
}
